<?php

class RecommendToUserController extends GxController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('Create', 'SaveRecommendtoFreind'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array(''),
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionCreate($id) {
        $model = new RecommendToUser;
        $recommendFromUser = new RecommendFromUser;
        $classDetails = '';
        $profileDetails = '';
        $userId = getLoginUserId();
        if (!empty($userId)) {
            $profileDetails = Users::getFrontEndUser($userId);
        }
        if ($_GET['type'] == "classes") {
            $classDetails = Classes::model()->findByPk($id);
        } else {
            $classDetails = Courses::model()->findByPk($id);
        }
        if (!empty($classDetails)) {
            $this->render('_form', array('model' => $model, 'recommendFromUser' => $recommendFromUser, 'classDetails' => $classDetails, 'profileDetails' => $profileDetails, 'classcoursetype' => $_GET['type']));
        }
        else
            throw new CHttpException(400, Yii::t('app', Message::Invalid_Request));
    }

    public function actionSaveRecommendtoFreind() {
        $recommendFromUser = new RecommendFromUser;
        if (isset($_POST['RecommendToUser'])) {
            $recommendFromUser->attributes = $_POST['RecommendFromUser'];
            if ($_POST['classcoursetype'] == "classes") {
                $classes = Classes::model()->findByPk($_POST['RecommendFromUser']['classes_id']);
                $classtitle = $classes->class_titile;
                $classcourseid = $classes->id;
                $actionurl='workshop-'.$classes->id.'/'.$classes->class_seo_title;
            } else {
                $classes = Courses::model()->findByPk($_POST['RecommendFromUser']['courses_id']);
                $classtitle = $classes->course_titile;
                $classcourseid = $classes->id;
                $actionurl='courses-'.$classes->id.'/'.$classes->course_seo_title;
            }
            $from_user_name = $_POST['RecommendFromUser']['from_name'];
            $mail_from = $_POST['RecommendFromUser']['from_email'];
            if ($recommendFromUser->save(FALSE)) {
                for ($key = 0; $key < count($_POST['RecommendToUser']['to_email']); $key++) {
                    $model = new RecommendToUser;
                    $model->setAttribute('to_name', $_POST['RecommendToUser']['to_name'][$key]);
                    $model->setAttribute('to_email', $_POST['RecommendToUser']['to_email'][$key]);
                    $model->setAttribute('to_phone_no', $_POST['RecommendToUser']['to_phone_no'][$key]);
                    $model->setAttribute('to_message_text', $_POST['RecommendToUser']['to_message_text']);
                    $model->setAttribute('from_user_id', $recommendFromUser->id);
                    if ($model->save(FALSE)) {
                        $mailmodel = EmailTemplate::model()->findByPk(Message::RECOMMEND_A_FRIEND);
                        $userText = $_POST['RecommendToUser']['to_message_text'] . '<br/>'; 
                        $mailbody = array('value' => $mailmodel->body, 'type' => 'raw');                       
                        $userName = $_POST['RecommendToUser']['to_name'][$key];
                        $mailto = $_POST['RecommendToUser']['to_email'][$key];
                        $classCourseName = $classtitle;
                        $classCourseViewdetailsLink = '<a href="http://' . $_SERVER['HTTP_HOST'] . $this->createUrl($actionurl). '" class="linkclass" target="_blank">View ' . SkillConfig::CLASS_WORK_SHOP . '</a>';
                        $arrayFind = array('[username]', '[from_user_name]', '[from_use_email]', '[classcoursename]', '[user_text]','[classCourseViewdetailsLink]');
                        $arrayReplace = array($userName, $from_user_name, $mail_from, $classCourseName,$userText, $classCourseViewdetailsLink);
                        $mailbody = str_replace($arrayFind, $arrayReplace, $mailbody);
                        $mailtemp = Template::model()->findByPk($mailmodel->template_id);
                        $subject = $_POST['RecommendToUser']['to_name'][$key].', '.$mailmodel->subject;
                        $body = $this->renderPartial('/mails/' . $mailtemp->filename, array('mailbody' => $mailbody['value']), true, false);
                      
                        try {
                            SkillSendMail::SendMail($mailto, $subject, $body);
                        } catch (Exception $e) {
                            echo $e;
                            exit;
                        }
                    }
                }
            }
        }
        echo 'success';
        die;
    }

}